package com.lbd.bankcard.controller;

import com.lbd.bankcard.bean.BankCard;
import com.lbd.bankcard.service.BankCardService;
import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zzl on 2016/10/19.
 */
@RestController
@RequestMapping("/bankcard")
public class BankCardController extends BaseController {
    @Autowired
    private BankCardService bankcardService;

    @Value("${nginxUrl}")
    String nginxUrl;

    /**
     * 按条件分页查询bankcard
     *
     * @param page     第几页
     * @param rows     一页多少条
     * @param bankcard
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, BankCard bankcard) {
        Page<BankCard> bankCardPage = bankcardService.findByPageConditionJpql(page, rows, bankcard);
        bankCardPage.getContent().stream().forEach(bankCard -> bankCard.setIconUrl(nginxUrl + "/" + bankCard.getIconUrl()));
        return new Result(ResultState.SUCCESS, "查询成功", bankcardService.findByPageConditionJpql(page, rows, bankcard));
    }

    /**
     * 保存bankcard信息
     *
     * @param bankcard
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(BankCard bankcard) {
        bankcardService.save(bankcard);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 根据id查询回显要编辑的BankCard
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", bankcardService.findById(id));
    }

    /**
     * 修改bankcard信息
     *
     * @param bankcard
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(BankCard bankcard) {
        bankcardService.update(bankcard);
        return new Result(ResultState.SUCCESS, "更新成功");
    }


}
